package com.jianxin.service;


import com.jianxin.spring.JianxinApplicationContext;

public class Test {

    public static void main(String[] args) {

        //spring本身得到容器要么传入配置类，要么传入配置文件，这里我们采用传入配置类的方式模拟
        JianxinApplicationContext applicationContext = new JianxinApplicationContext(AppConfig.class);

//        getBeanByName(applicationContext);
//        testScope(applicationContext);
//        testAutowired(applicationContext);
        testBeanPostProcessor(applicationContext);
    }

    /**
     * 测试依赖注入 - 存在循环依赖问题
     */
    private static void testAutowired(JianxinApplicationContext applicationContext) {
        UserService userService = (UserService) applicationContext.getBean("userService");
//        OrderService orderService = (OrderService) applicationContext.getBean("userService");
        userService.test();
//        orderService.test();
    }

    /**
     * 通过修改@scope测试单例or多例
     * @param applicationContext
     */
    private static void testScope(JianxinApplicationContext applicationContext) {
        System.out.println(applicationContext.getBean("userService"));
        System.out.println(applicationContext.getBean("userService"));
        System.out.println(applicationContext.getBean("userService"));
        System.out.println(applicationContext.getBean("userService"));
    }

    /**
     * 模拟spring从容器中根据beanName获取对象
     * @param applicationContext
     */
    private static void getBeanByName(JianxinApplicationContext applicationContext) {
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.test();
    }

    /**
     * 测试BeanPostProcessor
     * @param applicationContext
     */
    private static void testBeanPostProcessor(JianxinApplicationContext applicationContext) {
        //注意 jdk动态代理返回的对象是接口对象
        //这里如果用了动态代理那么返回的是UserInterface对象而不是UserService所以会报强制转化错误，改成UserInterface就可以了
        UserInterface userService = (UserInterface) applicationContext.getBean("userService");
        userService.test();
    }

}
